package connection

import (
	"sync"
)

/***************************** websocket handler 函数注册器 ***********************************/
type DisposeFunc func(connection *Connection, message []byte) (code int64, msg string, data interface{})

var (
	handlers        = make(map[string]DisposeFunc)
	handlersRWMutex sync.RWMutex
)

// 注册
func Register(key string, value DisposeFunc) {
	handlersRWMutex.Lock()
	defer handlersRWMutex.Unlock()
	handlers[key] = value
}

func GetHandler(key string) (fn DisposeFunc, ok bool) {
	handlersRWMutex.RLock()
	defer handlersRWMutex.RUnlock()
	fn, ok = handlers[key]
	return
}
